package com.datamation.swadeshi.view;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.SetTitleAdapter;

import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class FragmentInventoryControlDamageMain extends Fragment implements TabListener {
	View view;
	private ActionBar actionBar;
	
	FragmentInventoryControlDamageHeader tab2;
	FragmentInventoryControlDamageDetail tab3;
	FragmentInventoryControlDamageSummery tab4;
	
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.inventory_control_damage, container, false);
		
		setHasOptionsMenu(true);
		
		actionBar = getActivity().getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
        //set title
		SetTitleAdapter objTitle =new SetTitleAdapter();
		objTitle.createCutomActionBarTitle("Damage Transfer-New Damage", getActivity());
		
		MainActivity activity = (MainActivity) getActivity();
				
		//adding tabs

        
        Tab aTab2 = actionBar.newTab();
        aTab2.setText("Header");
        aTab2.setTabListener(this);
        actionBar.addTab(aTab2);
        
        Tab aTab3 = actionBar.newTab();
        aTab3.setText("Detail");
        aTab3.setTabListener(this);
        actionBar.addTab(aTab3);
          
        Tab aTab4 = actionBar.newTab();
        aTab4.setText("Summery");
        aTab4.setTabListener(this);
        actionBar.addTab(aTab4);
        
              
		//back button disable 
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setOnKeyListener(new View.OnKeyListener() {
		        @Override
		        public boolean onKey(View v, int keyCode, KeyEvent event) {
		            Log.i("", "keyCode: " + keyCode);
		            if( keyCode == KeyEvent.KEYCODE_BACK ) 
		            {
		            	Toast.makeText(getActivity(),"Back Navigationid is disable", Toast.LENGTH_SHORT).show();
		                //getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		                return true;
		            }else if ((keyCode == KeyEvent.KEYCODE_HOME)) {
		    			
		    			getActivity().finish();
		    			
		    			return true;

		    		} else {
		                return false;
		            }
		        }
		    });
		
	
		return view;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) 
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		int selectedTabIndex = tab.getPosition();
		if (selectedTabIndex == 0) 
		{
			tab2 = new FragmentInventoryControlDamageHeader();
			ft.replace(R.id.inventoryloading, tab2);
		}
		else if (selectedTabIndex == 1) 
		{
			tab3 = new FragmentInventoryControlDamageDetail();
			ft.replace(R.id.inventoryloading, tab3);
		}
		else if (selectedTabIndex == 2)
		{
			tab4 = new FragmentInventoryControlDamageSummery();
			ft.replace(R.id.inventoryloading, tab4);
		}
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}


}
